package com.chc.web.user_complaint.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chc.utils.Msg;
import com.chc.web.user_complaint.entity.Update;
import com.chc.web.user_complaint.entity.UserComplaint;
import com.chc.web.user_complaint.entity.UserComplaintParm;
import com.chc.web.user_complaint.service.UserComplaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/01/20/14:04
 * @Description: 投诉管理模块
 */
@RestController
@RequestMapping("/api/userComplaint")
public class UserComplaintController {
    @Autowired
    private UserComplaintService userComplaintService;

    /**
     * 查询
     * */
    @GetMapping("/list")
    public Msg getList(@Valid UserComplaintParm parm){
        //构造条件
        QueryWrapper<UserComplaint> query = new QueryWrapper();
        query.lambda().like(UserComplaint::getTitle,parm.getTitle())
                .like(UserComplaint::getComplaintContent,parm.getComplaintContent());
        //构造分页对象
        IPage<UserComplaint> page = new Page<>();
        page.setCurrent(parm.getCurrentPage());
        page.setSize(parm.getPageSize());
        IPage<UserComplaint> list = userComplaintService.page(page, query);
        return Msg.success("查询成功",list);
    }

    /**
     * 我的投诉查询
     * */
    @GetMapping("/myList")
    public Msg getMyList(@Valid UserComplaintParm parm){
        //构造条件
        QueryWrapper<UserComplaint> query = new QueryWrapper();
        query.lambda().like(UserComplaint::getTitle,parm.getTitle())
                .like(UserComplaint::getComplaintContent,parm.getComplaintContent()).eq(UserComplaint::getUserId,parm.getUserId());
        //构造分页对象
        IPage<UserComplaint> page = new Page<>();
        page.setCurrent(parm.getCurrentPage());
        page.setSize(parm.getPageSize());
        IPage<UserComplaint> list = userComplaintService.page(page, query);
        return Msg.success("查询成功",list);
    }

    /**
     * 新增
     * */
    @PostMapping
    @PreAuthorize("hasAuthority('sys:MyuserComplaintList:add')")
    public Msg add(@RequestBody @Valid UserComplaint userComplaint){
        //设置投诉状态
        userComplaint.setStatus("0");
        //设置投诉时间
        userComplaint.setCreateTime(new Date());
        //保存数据到数据库
        boolean save = userComplaintService.save(userComplaint);
        if(save){
            return Msg.success("投诉成功!");
        }
        return Msg.error("投诉失败!");
    }

    /**
     * 编辑
     * */
    @PutMapping
    @PreAuthorize("hasAnyAuthority('sys:MyuserComplaintList:edit','sys:userComplaintList:do')")
    public Msg edit(@RequestBody @Validated({Update.class}) UserComplaint userComplaint){
        //设置时间
        userComplaint.setCreateTime(new Date());
        //保存数据到数据库
        boolean save = userComplaintService.updateById(userComplaint);
        if(save){
            return Msg.success("编辑成功!");
        }
        return Msg.error("编辑失败!");
    }

    /**
     * 删除
     * */
    @DeleteMapping("/{complaintId}")
    @PreAuthorize("hasAuthority('sys:MyuserComplaintList:delete')")
    public Msg delete(@PathVariable("complaintId") Long complaintId){
        //保存数据到数据库
        boolean save = userComplaintService.removeById(complaintId);
        if(save){
            return Msg.success("删除成功!");
        }
        return Msg.error("删除失败!");
    }
}
